package com.game.league.entrance.splash;

import android.text.TextUtils;
import android.view.KeyEvent;

import com.game.ad.callback.AdCallBack;
import com.game.ad.xiaom.XiaoMiMgr;
import com.game.base.app.App;
import com.game.base.app.base.BaseActivity;
import com.game.base.server.base.BmobManager;
import com.game.base.utils.psp.PSP;
import com.game.base.utils.time.FormatPattern;
import com.game.base.utils.time.TimeUtil;
import com.game.league.R;
import com.game.league.databinding.ActivitySplashBinding;
import com.game.league.entrance.user.UserMgr;
import com.game.league.entrance.user.table.User;
import com.game.league.service.InterfaceMgr;
import com.game.league.util.Constants;
import com.game.league.util.SkipUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * Created by Su on 2019/5/21.
 */
public class SplashActivity extends BaseActivity<ActivitySplashBinding> {
    //设置一个变量来控制当前开屏页面是否可以跳转
    private boolean canJump = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        BmobManager.getServerTime(new QueryListener<Long>() {
            @Override
            public void done(Long aLong, BmobException e) {
                if (e == null) {
                    resetServerCoinToday();
                }
            }
        });  // 更新本地服务器时间
        InterfaceMgr.getInstance().getServerConfig();  // 获取在线配置
        UserMgr.fetchUserInfo();  // 更新用户信息缓存
    }

    @Override
    protected void onStart() {
        super.onStart();
        XiaoMiMgr.showSplashAd(this, binding.splashContainer, new AdCallBack() {
            @Override
            public void onAdDismissed() {
                next();
            }

            @Override
            public void onAdFailed() {
                binding.layout.postDelayed(SplashActivity.this::next, 2500);
            }
        });
    }

    /**
     * 设置一个变量来控制当前开屏页面是否可以跳转，当开屏广告为普链类广告时，点击会打开一个广告落地页，此时开发者还不能打开自己的App主页。
     * 当从广告落地页返回以后， 才可以跳转到开发者自己的App主页；当开屏广告是App类广告时只会下载App。
     */
    private void next() {
        if (canJump) {
            skip();
            return;
        }
        canJump = true;
    }

    private void skip() {
        if (UserMgr.isLogin() && UserMgr.getCurrentUser() != null) {
            SkipUtil.showMainClearTask(this);
            return;
        }
        SkipUtil.showLoginActivity(this);
    }

    /**
     * 重置服务端今日点券
     */
    private void resetServerCoinToday() {
        if (!UserMgr.isLogin()) return;

        String saveDate = PSP.getInstance().getString(Constants.KEY_TODAY_DATE, "");
        String serverDate = TimeUtil.formatTime(App.time, FormatPattern.FP_YMD2);
        if (TextUtils.equals(saveDate, serverDate)) {
            return;
        }
        PSP.getInstance().put(Constants.KEY_TODAY_DATE, serverDate);
        User user = UserMgr.getCurrentUser();
        user.setCoinToday(0f);
        UserMgr.updateUser(user);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (canJump) {
            next();
        }
        canJump = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        canJump = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        XiaoMiMgr.recycleSplash();
    }

    // ============= 开屏页最好禁止用户对返回按钮的控制 ======================
    @Override
    public void onBackPressed() {
        // 空实现，不响应返回键点击
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                || keyCode == KeyEvent.KEYCODE_HOME) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
